<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>角色分配权限</title>
        <link href="../../component/pear/css/pear.css" rel="stylesheet" />
	</head>
	<body class="pear-container">
		<div class="layui-row layui-col-space10">

            <input id="aid" value="" style="display: none">
            <form style="" class="layui-form" action="javascript:void (0)">

                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">权限选择</div>
                        <button class="pear-btn pear-btn-md pear-btn-primary query" lay-submit lay-filter="user-query"
                                id="commonTree3_btn" style="display: none;">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>

                        <div class="layui-card-body">
                            <ul id="demoTree3" class="dtree" data-id="0"></ul>
                        </div>
                    </div>
                </div>

                <div class="bottom" style="display: none">
                    <div class="button-container">
                        <button class="pear-btn pear-btn-primary pear-btn-sm" id="user-save" lay-filter="user-save">
                            <i class="layui-icon layui-icon-ok"></i>
                            提交
                        </button>
                    </div>
                </div>
            </form>



		</div>
		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
        <script src="../../js/baseUrl.js"></script>
        <script src="../../js/cookieUtils.js"></script>
        <script src="../../component/pear/module/ajaxRequest.js"></script>
        <script src="../../js/jquery-3.2.1.min.js"></script>
        <script src="../../js/permission.js"></script>
		<script>
			layui.use(['dtree', 'jquery','element','code','toast','ajaxRequest'], function () {
				var $ = layui.jquery,
					dtree = layui.dtree,
                    ajaxRequest = layui.ajaxRequest,
                    toast = layui.toast;
					
					layui.code();

                var rid = '';
                $(function () {
                    rid = $('#aid').val();
                    var commonTree3 = dtree.render({
                        elem: "#demoTree3",
                        initLevel: "1",
                        method: 'get',
                        url: baseUrl + "/admin/role/getSysRolePermission?rid="+rid,
                        dataFormat: "list", //配置data的风格为list
                        checkbar: true, //开启复选框
                        none: "没有数据",
                        // 开启菜单栏
                        menubar:true,
                        menubarTips:{
                            // group:["moveDown", "moveUp", "refresh", "checkAll", "unCheckAll", "invertAll", "remove", "searchNode"]
                            group:["moveDown", "moveUp", "refresh", "checkAll", "unCheckAll", "invertAll", "searchNode"]
                        }
                    });
                });


                // 点击提交时 调用commonTree3_btn的点击事件
                $("#user-save").click(function () {
                    $('#commonTree3_btn').click();
                });

                // 点击按钮获取所有选中节点值
                $("#commonTree3_btn").click(function(){
                    var params = dtree.getCheckbarNodesParam("demoTree3");

                    let loading = layer.load();

                    pids = "";
                    $.each(params,function(index,value){
                        if (index == params.length -1 ) {
                            pids = pids + value.nodeId;
                        }else {
                            pids = pids + value.nodeId + ",";
                        }
                        console.log(value.nodeId);
                    });
                    console.log(pids);
                    var res = ajaxRequest.sendPost("/admin/role/addSysRolePermission",{'rid':rid,'pids':pids},false,true);
                    if (res){
                        setTimeout(function() {
                            layer.close(loading);
                            toast.success({title: 'Success',message: res.msg,position: 'topRight'});
                            parent.layer.close(parent.layer.getFrameIndex(window
                                .name)); //关闭当前页
                        }, 1500);
                    }

                    // layer.msg(JSON.stringify(params));
                });




			});
		</script>
	</body>
</html>